<?php
/**
 * Created by PhpStorm.
 * User: Leonly
 * Date: 2015/9/24 0024
 * Time: 10:46
 */

/**
 * Class PostAction
 * 发货控制器
 */
class PostAction extends backendAction{
    public function _initialize() {
        parent::_initialize();
        $this->_mod = D('item_orders');
    }

    public function add() {
        $orderId= $this->_get('orderId', 'trim');
        !$orderId && $this->_404();

        if(IS_POST) {
            $deliver = D('Deliver');
            if($deliver->create($_POST)) {
                $deliver->created_at = date('Y/m/d H:i:s');
                if($deliver->add() ) {
                    $this->_mod->where("id=$orderId")->save(array('status'=> 2));
                    $this->success('发货成功', U('item_order/edit', array('id' => $orderId)));
                }
            }

            $this->error('发货失败：'. $deliver->getError());

        } else {
            $order = $this->_mod->where('id='.$orderId)->find();
            !$order && $this->error('订单不存在');

            if(! $order['status'] == 1) {//订单状态
                $this->error('订单非等待发货状态');
            }

            //默认地址
            $addr = M('addr')->where(array('id'=>$order['addr'], 'uid'=>$order['uid']))->find();

            $this->assign('order', $order);
            $this->assign('addr', $addr);
            $this->display();
        }
    }
}